package com.lightsaber.trade.as.dao.programme;

import java.util.List;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lightsaber.trade.as.entity.programme.BaseProgramme;
import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.as.entity.programme.ProgrammePlanProjectionType;
import com.lightsaber.trade.core.dao.BaseDao;

@Component
public class ProgrammePlanProjectionTypeDao extends BaseDao<ProgrammePlanProjectionType, String> {

    private final static Logger log = LoggerFactory.getLogger(ProgrammePlanProjectionTypeDao.class);

    public void deleteByProgrammeId(final Programme programme) {
        Query q = getSession().createQuery(
                "delete from ProgrammePlanProjectionType as pt where pt.programme=:programme");
        q.setParameter("programme", programme);

        int cnt = q.executeUpdate();
        log.debug("### Delete {} ProgrammePlanProjectionType by programmeId '{}'", new Object[] {
                cnt, programme.getId() });
    }

    public List<ProgrammePlanProjectionType> findByProgramme(final BaseProgramme programme) {
        eq("programme", programme);
        return list();
    }

    public ProgrammePlanProjectionType findByProgrammeAndYearWithoutSub(
            final BaseProgramme programme, final int year) {
        eq("programme.id", programme.getId());
        eq("year", year);
        isNull("subjectGroupPorgramme");
        return unique();
    }

    public void deleteFirstYearFromReceiver(final Programme p) {
        String hql = "delete from ProgrammePlanProjectionType pt where pt.year=1 and pt.programme=:p";

        createQuery(hql).setEntity("p", p).executeUpdate();
    }

    public void deleteOwedByFeeder(final Programme p) {
        String hql = "delete from ProgrammePlanProjectionType pt where pt.programme=:p and pt.subjectGroupPorgramme is null";

        createQuery(hql).setEntity("p", p).executeUpdate();
    }

    public void deleteByProgramme(final Programme p) {
        String hql = "delete from ProgrammePlanProjectionType pt where pt.programme=:p";

        createQuery(hql).setEntity("p", p).executeUpdate();
    }

    public ProgrammePlanProjectionType findByProgrammeIdAndSubjectGroupIdAndYear(
            final String programmeId, final String subjectGroupId, final int year) {
        eq("programme.id", programmeId);
        eq("year", year);
        eq("subjectGroupPorgramme.id", subjectGroupId);
        return unique();
    }

    public void deleteByProgrammeAndYear(final Programme dbProgramme, final Integer year) {
        Query q = getSession().createQuery(
                "delete from ProgrammePlanProjectionType as pt "
                        + "where pt.programme=:programme and pt.year=:year");
        q.setParameter("programme", dbProgramme);
        q.setParameter("year", year);

        int cnt = q.executeUpdate();
        log.debug("### Delete {} ProgrammePlanProjectionType by programmeId '{}' and Year '{}'",
                new Object[] { cnt, dbProgramme.getId(), year });

    }

    public ProgrammePlanProjectionType findByProgrammeAndSubjectGroupPorgramme(
            final Programme programme, final Programme subjectGroupPorgramme) {
        eq("programme", programme);
        eq("subjectGroupPorgramme", subjectGroupPorgramme);

        return unique();
    }

    public ProgrammePlanProjectionType findByProgrammeAndYear(Programme programme, int year) {
        eq("programme", programme);
        eq("year", year);

        return unique();
    }

    public void deleteByProgrammeAndSubjectGroupPorgramme(Programme programme,
            Programme subjectGroupPorgramme) {
        String hql = "delete from ProgrammePlanProjectionType where programme=:programme and subjectGroupPorgramme=:subjectGroupPorgramme";

        this.createQuery(hql).setEntity("programme", programme)
                .setEntity("subjectGroupPorgramme", subjectGroupPorgramme).executeUpdate();

    }
}
